package com.example.smartcanteen.controller;

import com.example.smartcanteen.utils.AliOss;
import com.example.smartcanteen.utils.ResponseMessage;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

@CrossOrigin
@RestController
public class uploadFileController {
    @PostMapping("/upload")
    public ResponseMessage upload(MultipartFile file) throws Exception {
        ResponseMessage responseMessage=new ResponseMessage();
        String originalFilename = file.getOriginalFilename();
        String filename = UUID.randomUUID().toString() + originalFilename.substring(originalFilename.lastIndexOf("."));
        String url= AliOss.uploadFile(filename,file.getInputStream());
        responseMessage.setMessage(url);
        System.out.println(responseMessage.getMessage());
        return responseMessage;
    }
}
